package com.yong.android.framework.view.recyclerview;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.ViewGroup;

/**
 * Description:
 * Author:yong
 * Date:2017-12-21
 */

public abstract class LoadingMoreCell<VH extends RecyclerView.ViewHolder> {
    boolean isEnable = false;
    private OnScrollListener mOnScrollListener;
    CellListAdapter mHostAdapter;

    protected abstract VH onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType);


    protected abstract void onBindViewHolder(VH holder);

    public int getViewType() {
        return 0;
    }

    protected abstract void onVisible(VH holder);

    void onAttachedToRecyclerView(RecyclerView recyclerView) {
        if (mOnScrollListener == null) {
            mOnScrollListener = new OnScrollListener();
        }
        recyclerView.addOnScrollListener(mOnScrollListener);
    }

    void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        if (mOnScrollListener != null) {
            recyclerView.removeOnScrollListener(mOnScrollListener);
        }
    }

    public void setEnable(boolean enable) {
        isEnable = enable;
    }

    public boolean isEnable() {
        return isEnable;
    }

    protected CellListAdapter getAdapter() {
        return mHostAdapter;
    }

    private class OnScrollListener extends RecyclerView.OnScrollListener {
        private int lastVisibleItemPosition;


        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            if (!isEnable()) {
                return;
            }
            final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
            if (layoutManager == null) {
                return;
            }
            if (layoutManager instanceof LinearLayoutManager) {
                //GridLayoutManager LinearLayoutManager
                lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
            } else if (layoutManager instanceof StaggeredGridLayoutManager) {
                StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
                final int[] lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
                staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
                lastVisibleItemPosition = findLastPosition(lastPositions);
            }
        }

        @SuppressWarnings("unchecked")
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            if (!isEnable()) {
                return;
            }
            super.onScrollStateChanged(recyclerView, newState);
            final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
            if (layoutManager == null) {
                return;
            }
            if (layoutManager.getChildCount() > 0
                    && newState == RecyclerView.SCROLL_STATE_IDLE
                    && lastVisibleItemPosition == layoutManager.getItemCount() - 1) {
                onVisible((VH) recyclerView.findViewHolderForAdapterPosition(lastVisibleItemPosition));
            }
        }

        private int findLastPosition(int[] lastPositions) {
            int max = lastPositions[0];
            for (int value : lastPositions) {
                if (value > max) {
                    max = value;
                }
            }
            return max;
        }
    }
}
